package com.sword.controller.stock;

import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.page.Result;
import com.sword.pojo.stock.BackstockDetail;
import com.sword.pojo.stock.Enterstock;
import com.sword.pojo.stock.EnterstockDetail;
import com.sword.service.impl.stock.EnterStockDetailServiceImpl;
import com.sword.service.stock.EnterStockService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stock")
public class EnterStockController {
    @Autowired
    private EnterStockService enterstockService;
    @Autowired
    private EnterStockDetailServiceImpl enterStockDetailService;

    @RequestMapping("/enterstock")
    public String enterstock() {
        return "views/stock/enterStock";
    }

    @RequestMapping("/enterstockdetail")
    public String enterstockdetail() {
        return "views/stock/enterStockDetail";
    }

    /**
   查询所有入库单
    */
    @RequestMapping("/getEnterstocks")
    @ResponseBody
    public PageResult queryEnterStock(@RequestBody QueryPageBean queryPageBean) {
        try {
            return enterstockService.queryEnterStockStock(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_ENTERSTOCK_FAIL, null, null);
        }
    }
    /**
     * 添加入库单功能
     */

    @ResponseBody
    @RequestMapping("/addEnterstock")
    public Result addEnterstock(@RequestBody Enterstock enterStock) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            enterStock.setEmployeeid(userId);
            // 判断是否添加成功
            Integer flag = enterstockService.addEnterStockStock(enterStock);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_ENTERSTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_ENTERSTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_ENTERSTOCK_SUCCESS, null);
    }
    /**
     * 修改入库单
     */
    @ResponseBody
    @RequestMapping("editEnterstock")
    public Result editEnterstock(@RequestBody Enterstock enterStock) {
        try {
            Integer flag = enterstockService.editEnterStockStock(enterStock);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCK_SUCCESS, null);
    }

    /**
     * 删除入库单
     */

    @ResponseBody
    @RequestMapping("deleteEnterstock")
    public Result deleteEnterstock(Integer id) {
        try {
            Integer flag = enterstockService.deleteEnterStockStock(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_ENTERSTOCK_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_ENTERSTOCK_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_ENTERSTOCK_SUCCESS, null);
    }


    /**
    查询所有入库明细
 */
    @RequestMapping("/getEnterstockdetails")
    @ResponseBody
    public PageResult queryEnterstockdetail(@RequestBody QueryPageBean queryPageBean) {
        try {
            return enterStockDetailService.queryEnterStockDetail(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(500, MessageConstant.QUERY_ENTERSTOCKDETAIL_FAIL, null, null);
        }
    }
    /**
     * 添加入库明细
     */

    @ResponseBody
    @RequestMapping("/addEnterstockdetail")
    public Result addEnterstockdetail(@RequestBody EnterstockDetail enterstockDetail) {
        try {
            // 获取session保存进数据
            String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
            enterstockDetail.setCreateid(userId);
            enterstockDetail.setStatus(0);
            // 判断是否添加成功
            Integer flag = enterStockDetailService.addEnterStockDetail(enterstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.ADD_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.ADD_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.ADD_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 修改入库明细
     */
    @ResponseBody
    @RequestMapping("editEnterstockdetail")
    public Result editEnterstockdetail(@RequestBody EnterstockDetail enterstockDetail) {
        try {
            Integer flag = enterStockDetailService.editEnterStockDetail(enterstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 审核入库明细
     */
    @ResponseBody
    @RequestMapping("examineEnterstockdetail")
    public Result examineEnterstockdetail(@RequestBody EnterstockDetail enterstockDetail) {
        try {
            Integer flag = enterStockDetailService.examineEnterStockDetail(enterstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 弃审入库明细
     */
    @ResponseBody
    @RequestMapping("unexamineEnterstockdetail")
    public Result unexamineEnterstockdetail(@RequestBody EnterstockDetail enterstockDetail) {
        try {
            Integer flag = enterStockDetailService.unexamineEnterStockDetail(enterstockDetail);
            if (flag <= 0) {
                return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.EDIT_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.EDIT_ENTERSTOCKDETAIL_SUCCESS, null);
    }

    /**
     * 删除入库明细
     */

    @ResponseBody
    @RequestMapping("deleteEnterstockdetail")
    public Result deleteEnterstockdetail(Integer id) {
        try {
            Integer flag = enterStockDetailService.deleteEnterStockDetail(id);
            if (flag <= 0) {
                return new Result(500, MessageConstant.DELETE_ENTERSTOCKDETAIL_FAIL, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(500, MessageConstant.DELETE_ENTERSTOCKDETAIL_FAIL, null);
        }
        return new Result(200, MessageConstant.DELETE_ENTERSTOCKDETAIL_SUCCESS, null);
    }
    /**
     * 查询数据用于可视化
     */
    @ResponseBody
    @RequestMapping("/getstocks")
    public Map getstocks() {
        List<Enterstock> employees = enterstockService.queryAll();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data",employees);
        return map;
    }
}
